﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using eeenno.ReaderEngine.Objects;
using eeenno.ReaderEngine.Utilities;


namespace eeenno.ReaderEngine.Helpers
{
    public class ArticleHelper
    {
        private const char _DATE_TIME_SPLIT = 'T';
        private const char _DATE_SPLIT = '-';
        private const char _TIME_SPLIT = ':';

        public static List<Article> BuildList(string[,] articles)
        {
            List<Article> ret_val = new List<Article>(articles.GetLength(0));

            for (int i = 0; i < ret_val.Capacity; i++)
            {
                Article article = new Article();
                article.title = FilterSymbols(articles[i, 0]);
                article.date = ArticleHelper.ConvertUnixTimetoDateTime(articles[i, 1]);
                article.id = articles[i, 2];
                article.tag = ArticleHelper.GetTags(articles[i, 3]);
                article.url = articles[i, 4];
                article.author = FilterSymbols(articles[i, 6]);
                article.unread = ArticleHelper.GetUnread(article.tag);
                article.starred = ArticleHelper.GetStarred(article.tag);
                article.site_url = articles[i, 7];
                article.site_name = FilterSymbols(articles[i, 8]);

                StringBuilder sb = new StringBuilder(AddArticleTitle(article.title, article.url));
                sb.Append(AddArticleSiteName(article.site_name));
                sb.Append("<br />");
                sb.Append(AddArticleAuthor(article.author));
                sb.Append("<br />");
                sb.Append(articles[i, 5]);
                sb.Append("<br />");
                sb.Append("<br />");
                article.summary = FilterSymbols(sb.ToString());
                if (Settings.download_read_articles || article.unread)
                {
                    ret_val.Add(article);
                }
            }

            return ret_val;
        }

        private static string AddArticleAuthor(string author)
        {
            StringBuilder sb = new StringBuilder("by ");
            sb.Append(author);
            return sb.ToString();
        }

        private static string AddArticleSiteName(string sitename)
        {
            StringBuilder sb = new StringBuilder("<b>");
            sb.Append(sitename);
            sb.Append("</b>");
            return sb.ToString();
        }

        private static string AddArticleTitle(string title, string link)
        {
            StringBuilder sb = new StringBuilder("<h3><a href=\"");
            sb.Append(link);
            sb.Append("\">");
            sb.Append(title);
            sb.Append("</a></h3>");
            return sb.ToString();
        }

        private static string AddArticleLink(string link)
        {
            StringBuilder sb = new StringBuilder("<h3><a href=\"");
            sb.Append(link);
            sb.Append("\">Link to article</a></h3>");
            return sb.ToString();
        }

        private static bool GetUnread(string data)
        {
            data = data.Replace("reading-list", "");
            if (data.ToLower().Contains("read") && !data.ToLower().Contains("tracking-kept-unread"))
            {
                return false;
            }
            else
            {
                return true;
            }
        }        

        private static bool GetStarred(string data)
        {
            if (data.ToLower().Contains("starred"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private static string GetTags(string data)
        {
            if (data[0].Equals('~'))
            {
                data = data.Remove(0, 1);
            }
            return data.Replace('~', ',');
        }

        private static string FilterSymbols(string input)
        {
            if (input.Contains("&#39;"))
            {
                input = input.Replace("&#39;", "'");
            }
            if (input.Contains("&amp;"))
            {
                input = input.Replace("&amp;", "&");
            }
            if (input.Contains("&gt;"))
            {
                input = input.Replace("&gt;", ">");
            }
            if (input.Contains("&lt;"))
            {
                input = input.Replace("&lt;", "<");
            }
            if (input.Contains("&quot;"))
            {
                input = input.Replace("&quot;", "\"");
            }
            if (input.Contains("<em>"))
            {
                input = input.Replace("<em>", "");
            }
            if (input.Contains("</em>"))
            {
                input = input.Replace("</em>", "");
            }
            return input;
        }

        /// <summary>
        /// Parses the data retrieved from Google feed data
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string ConvertUnixTimetoDateTime(string date)
        {
            int crawl_time = 0;
            try
            {
                crawl_time = (int)(double.Parse(date) / 1000);
            }
            catch (Exception e)
            {
                return "";
            }
            DateTime datetime = TimeZone.CurrentTimeZone.ToLocalTime(Settings.unix_datetime.AddSeconds(crawl_time));
            return datetime.ToString("MM/dd/yyyy hh:mm tt");
        }

        public static string ConvertDateTimeToUnixTime(string date, int additional_min)
        {
            DateTime datetime = new DateTime();
            try
            {
                datetime = TimeZone.CurrentTimeZone.ToUniversalTime(DateTime.Parse(date));
            }
            catch (Exception e)
            {
                datetime = new DateTime();
            }
            datetime = datetime.AddMinutes(additional_min);
            return (datetime - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds.ToString();
        }
    }
}
